package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrDict;
import com.cms.service.DictService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class DictManagerController extends BaseController{

	 @Autowired
	 private DictService dictService;
	 
	 @Autowired
	 private ReportService reportService;
	 
	 @RequestMapping("/dictManager.do")
	 public String enter(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 map.put("dictCatalogList", this.dictService.queryCatalog());
		 return "dict/dictMain.html";
	 }
	 
	 @RequestMapping("/dictManager/toaddDict.do")
	 public String toaddDict(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 map.put("dictCatalogList", this.dictService.queryCatalog());
		 map.put("action", "/dictManager/saveDict.do");
		 map.put("dict", new TrDict());
		 return "dict/dictInfo.html";
	 }
	 
	 @RequestMapping("/dictManager/toupdateDict.do")
	 public String toupdateDict(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 int dictId= StringUtils.str2Int(req.getParameter("dictId"));
		 TrDict dict = new TrDict();
		 dict.setId(dictId);
		 map.put("dictCatalogList", this.dictService.queryCatalog());
		 map.put("action", "/dictManager/updateDict.do");
		 map.put("dict", this.dictService.getDictInfo(dict));
		 return "dict/dictInfo.html";
	 }
	 
	 @RequestMapping("/dictManager/saveDict.do")
	 public void saveDict(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 TrDict dict =new TrDict();
		 TrAdminUser tu =this.getLoginUser(req);
		 dict.setCreateUser(tu.getId());
		 dict.setModifyUser(tu.getId());
		 this.export(req, "dict", dict);
		 Map<String, Object>param = new HashMap<String, Object>();
		 try
		 {
			 this.dictService.saveDict(dict);
			 param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 }
		 catch (Exception e) {
			 param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 param.put(CmsConstant.MSG, e.getMessage());
		 }
		 this.renderText(resp, new Gson().toJson(param));
	 }
	 
	 
	 @RequestMapping("/dictManager/updateDict.do")
	 public void updateDict(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 TrDict dict =new TrDict();
		 TrAdminUser tu =this.getLoginUser(req);
		 dict.setCreateUser(tu.getId());
		 dict.setModifyUser(tu.getId());
		 this.export(req, "dict", dict);
		 Map<String, Object>param = new HashMap<String, Object>();
		 try
		 {
			 this.dictService.updateDict(dict);
			 param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 }
		 catch (Exception e) {
			 param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 param.put(CmsConstant.MSG, e.getMessage());
		 }
		 this.renderText(resp, new Gson().toJson(param));
	 }
	 
	 @RequestMapping("/dictManager/delDict.do")
	 public void delDict(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 TrDict dict =new TrDict();
		 dict.setId(StringUtils.str2Int(req.getParameter("dictId")));
		 Map<String, Object>param = new HashMap<String, Object>();
		 try
		 {
			 this.dictService.delDict(dict);
			 param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 }
		 catch (Exception e) {
			 param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			 param.put(CmsConstant.MSG, e.getMessage());
		 }
		 this.renderText(resp, new Gson().toJson(param));
	 }
	 
	 
	 @RequestMapping("/dictManager/dictList.do")
	 public String dictList(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 Map<String,Object>param = this.buildQueryMap(req);
		 map.put("dictList", this.reportService.pagingReportResult("pagingDictData", param));
		 return "dict/dictList.html";
	 }
}
